<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function dd($data = []) {
    dump($data);die;
}

function getOutpageData($data){
    unset($data['page']);
    unset($data['_']);

    $data = array_filter($data, function ($item){
        if ($item) {
            return $item;
        }
        if (in_array($item,['0']) ) {
            return true;
        }
    });

    return $data;
}
# 生成where
function genWhereOrData($data,$fileds=[]){
    $whereOr = [];
    $whereand = [];
    foreach ($data as $key=>$val)
    {
        if ($key == 'searchval') {
            foreach ($fileds as $filedval) {
                $whereOr[] = [
                    [$filedval,'like', "%{$val}%"]
                ]  ;
            }
        } else {
            $whereand[$key] = $val;
        }

    }
    return ['and'=>$whereand, 'or'=>$whereOr ];
}

# make where 对象； $whereTeye or | and

function makeWhereObject(\think\Model $object,$whereData)
{
    if (!empty($whereData)) {
        if(!empty($whereData['or'])) {
            $object = $object->whereOr($whereData['or']);
        }
        if(!empty($whereData['and'])) {
            $object = $object->where($whereData['and']);
        }
    }

    return $object;
}

function returnData($data=[],$message='success',$status='success'){
    $returnData = [
        'data' => $data,
        'message' => $message,
        'status' => $status== 'success'? 1:0
    ];
    return json($returnData);
}


function kencry($data){
    if (is_object($data)) {
        $data = serialize($data);
    }
    $key = config('encrykey.key');
    $methodes = openssl_get_cipher_methods();
    $method = reset($methodes);
    $message = openssl_encrypt($data,$method,$key);
    return hash($message);
}



function productPath($product_id) {
    $dir_name = (new \think\Env())->get('root_path');
    if (!$dir_name) {
        $dir_name = app('env')->get('root_path');
    }
    $dir_product = $dir_name.'public/make/product/'.$product_id;
    if(!is_dir($dir_product)) {
        mkdir($dir_product, 0777, true);
    }
    return  $dir_product;
}

function userPath($user_id){
    $dir_name = (new \think\Env())->get('root_path');
    if (!$dir_name) {
        $dir_name = app('env')->get('root_path');
    }
    $dir_user = $dir_name.'public/make/user/'.$user_id;
    return  $dir_user;
}

function publicPath (){

    $dir_name = (new \think\Env())->get('root_path');
    if (!$dir_name) {
        $dir_name = app('env')->get('root_path');
    }
    $dir_public = $dir_name.'public';
    return  $dir_public;
}
